#import "MBProgressHUD+Manager.h"


@implementation MBProgressHUD (Manager)
+ (void)createHudWithMsg:(NSString *)msg
                   delay:(NSTimeInterval)delay
                  toView:(UIView *)view
                    icon:(id)icon
         completionBlock:(HUDCompletionBlock)completionBlock {
    if (view == nil) {
        view = [self getKeyWindow];
    }
    [self hideHUDForView:view animated:YES];
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
    hud.label.text = msg;
    hud.label.numberOfLines = 0;
    if (icon == nil) {
        hud.mode = MBProgressHUDModeText;
    } else {
        UIImage *img = [[UIImage alloc] init];
        if ([icon isKindOfClass:[NSString class]]) {
            img = [UIImage imageNamed:icon];
        } else {
            img = icon;
        }
        hud.customView = [[UIImageView alloc] initWithImage:img];
        hud.mode = MBProgressHUDModeCustomView;
    }
    hud.contentColor = [UIColor whiteColor];
    hud.bezelView.color = [UIColor blackColor];
    for (UIView *v in hud.bezelView.subviews) {
        if ([v isKindOfClass:[UIVisualEffectView class]]) {
            v.hidden = YES;
        }
    }
    [hud hideAnimated:YES afterDelay:delay];
    hud.completionBlock = ^{
        if (completionBlock != nil) {
            completionBlock();
        }
    };
}
#pragma mark - 显示一条信息
+ (void)showHUDMsg:(NSString *)msg toView:(UIView *_Nullable)view {
    [self showHUDMsg:msg toView:view completionBlock:nil];
}
+ (void)showHUDMsg:(NSString *)msg toView:(UIView *_Nullable)view completionBlock:(HUDCompletionBlock)completionBlock {
    [self createHudWithMsg:msg delay:duration toView:view icon:nil completionBlock:completionBlock];
}
#pragma mark - 显示成功信息
+ (void)showHUDSucc:(NSString *)msg toView:(UIView *_Nullable)view {
    [self showHUDSucc:msg toView:view completionBlock:nil];
}
+ (void)showHUDSucc:(NSString *)msg toView:(UIView *_Nullable)view completionBlock:(HUDCompletionBlock)completionBlock {
    [self createHudWithMsg:msg delay:duration toView:view icon:[self getImageFromBundleWithName:@"success"] completionBlock:completionBlock];
}
#pragma mark - 显示错误信息
+ (void)showHUDError:(NSString *)msg toView:(UIView *_Nullable)view {
    [self showHUDError:msg toView:view completionBlock:nil];
}
+ (void)showHUDError:(NSString *)msg toView:(UIView *_Nullable)view completionBlock:(HUDCompletionBlock)completionBlock {
    [self createHudWithMsg:msg delay:duration toView:view icon:[self getImageFromBundleWithName:@"error"] completionBlock:completionBlock];
}
#pragma mark - 显示警告信息
+ (void)showHUDWarning:(NSString *)msg toView:(UIView *_Nullable)view {
    [self showHUDWarning:msg toView:view completionBlock:nil];
}
+ (void)showHUDWarning:(NSString *)msg toView:(UIView *_Nullable)view completionBlock:(HUDCompletionBlock)completionBlock {
    [self createHudWithMsg:msg delay:duration toView:view icon:[self getImageFromBundleWithName:@"warning"] completionBlock:completionBlock];
}
#pragma mark - 菊花 样式
+ (MBProgressHUD *)showHUDActivityMsg:(NSString *_Nullable)msg {
    return [MBProgressHUD showHUDActivityMsg:msg toView:nil];
}
+ (MBProgressHUD *)showHUDActivityMsg:(NSString *_Nullable)msg toView:(UIView *_Nullable)view {
    return [MBProgressHUD showHUDProgressMsg:msg toView:view mode:MBProgressHUDModeIndeterminate];
}
#pragma mark - 进度 样式
+ (MBProgressHUD *)showHUDProgressMsg:(NSString *_Nullable)msg toView:(UIView *_Nullable)view mode:(MBProgressHUDMode)mode {
    if (view == nil) {
        view = [self getKeyWindow];
    }
    [self hideHUDForView:view animated:YES];
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
    hud.label.text = msg;
    hud.label.numberOfLines = 0;
    hud.mode = mode;
    hud.contentColor = [UIColor whiteColor];
    hud.bezelView.color = [UIColor blackColor];
    for (UIView *v in hud.bezelView.subviews) {
        if ([v isKindOfClass:[UIVisualEffectView class]]) {
            v.hidden = YES;
        }
    }
    return hud;
}
+ (void)hideHUDForView:(UIView *)view {
    if (view == nil) {
        view = [self getKeyWindow];
    }
    [self hideHUDForView:view animated:YES];
}
+ (void)hideHUD {
    [self hideHUDForView:nil];
}
+ (UIImage *)getImageFromBundleWithName:(NSString *)imageName {
    NSURL *bundleURL = [[NSBundle mainBundle] URLForResource:@"YHMBProgressHUDManager" withExtension:@"bundle"];
    if (bundleURL) {
        NSBundle *bundle = [NSBundle bundleWithURL:bundleURL];
        NSInteger scale = [[UIScreen mainScreen] scale];
        NSString *imgName = [NSString stringWithFormat:@"%@@%zdx.png", imageName, scale];
        return [UIImage imageWithContentsOfFile:[bundle pathForResource:imgName ofType:nil]];
    }
    return nil;
}

+ (UIWindow *)getKeyWindow {
    UIWindow *keyWindow;
    for (UIWindow *window in [UIApplication sharedApplication].windows) {
        if ([window isKeyWindow]) {
            keyWindow = window;
        }
    }
    return keyWindow;
}
@end
